home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / progutil / iostream.zoo / src / strstrea.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  3.8 KB  |  170 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #pragma implementation
  19. #include <strstrea.h>
  20. #include <std.h>
  21.  
  22. istrstream::istrstream(char *cp) : istream(NULL)
  23. {
  24.     _strbuf = new strstreambuf(cp, 0, NULL);
  25. }
  26.  
  27. istrstream::istrstream(char *cp, size_t n) : istream(NULL)
  28. {
  29.     _strbuf = new strstreambuf(cp, n, NULL);
  30. }
  31.  
  32. istrstream::~istrstream()
  33. {
  34.     delete _strbuf;
  35. }
  36.  
  37. ostrstream::ostrstream() : ostream(NULL)
  38. {
  39.     _strbuf = new strstreambuf;
  40. }
  41.  
  42. ostrstream::ostrstream(char *cp, size_t n, int mode) : ostream(NULL)
  43. {
  44.     char *pstart;
  45.     if (mode == ios::app || mode == ios::ate)
  46.     pstart = cp + strlen(cp);
  47.     else
  48.     pstart = cp;
  49.     _strbuf = new strstreambuf(cp, n, pstart);
  50. }
  51.  
  52.  
  53. ostrstream::~ostrstream()
  54. {
  55.     delete _strbuf;
  56. }
  57.  
  58. char *strstreambuf::str()
  59. {
  60.     freeze(1);
  61.     return buf;
  62. }
  63.  
  64. size_t strstreambuf::pcount()
  65. {
  66.     size_t put_len = pptr() - pbase();
  67.     if (put_len < *lenp) put_len = *lenp;
  68.     return put_len;
  69. }
  70.  
  71. int strstreambuf::overflow(int c = EOF)
  72. {
  73.   const int flush_only = c == EOF;
  74.   size_t pos = pptr() - pbase();
  75.   size_t get_pos = gptr() - pbase();
  76.   if (pos > *lenp) *lenp = pos;
  77.   if (pos >= *sizep /* + flush_only*/) {
  78.       char *new_buf;
  79.       size_t new_size = 2 * *sizep;
  80.       if (_frozen) /* not allowed to enlarge */
  81.       new_buf = NULL;
  82.       else
  83.       new_buf = (char *) realloc(buf, new_size);
  84.       if (new_buf == NULL) {
  85. //      __ferror(fp) = 1;
  86.       return EOF;
  87.       }
  88.       *sizep = new_size;
  89.       if (lenp == &_len) /* use '\0'-filling */
  90.       memset(new_buf + pos, 0, *sizep - pos);
  91.       buf = new_buf;
  92.       if (bufp) *bufp = buf;
  93.       _base = 0; // Hack to prevent delete of old buffer.
  94.       setb(new_buf, new_buf+*sizep, 1);
  95.     }
  96.  
  97.   setp(buf, buf + *sizep);
  98.   pbump(pos);
  99.   setg(buf, buf + get_pos, buf + *lenp);
  100.   if (!flush_only)
  101.       *_pptr++ = (unsigned char) c;
  102.   return c;
  103. }
  104.  
  105. strstreambuf::strstreambuf()
  106. {
  107.     _frozen = 0;
  108. //    _flags &= ~ios::dont_close; 
  109.     _len = 0;
  110.     _size = 128;
  111.     sizep = &_size;
  112.     lenp = &_len;
  113.     buf = (char*)malloc(_size);
  114.     bufp = &buf;
  115.     setb(buf, buf+_size);
  116.     setp(buf, buf+_size);
  117.     _flags |= _S_CAN_READ|_S_CAN_WRITE;
  118. }
  119.  
  120. strstreambuf::strstreambuf(int initial)
  121. {
  122.     _frozen = 0;
  123. //    _flags &= ~ios::dont_close; 
  124.     _len = 0;
  125.     if (initial < 16)
  126.     initial = 16;
  127.     _size = initial;
  128.     sizep = &_size;
  129.     lenp = &_len;
  130.     buf = (char*)malloc(_size);
  131.     bufp = &buf;
  132.     setb(buf, buf+_size);
  133.     setp(buf, buf+_size);
  134.     _flags |= _S_CAN_READ|_S_CAN_WRITE;
  135. }
  136.  
  137. strstreambuf::strstreambuf(char *ptr, size_t size, char *pstart = NULL)
  138. {
  139. //    _flags &= ~ios::dont_close; 
  140.     _frozen = 1;
  141.     _len = 0;
  142.     if (size == 0)
  143.     size = strlen(ptr);
  144.     else if (size < 0)
  145.     size = 0x7FFFFFFF; // !!!
  146.     _size = size;
  147.     sizep = &_size;
  148.     lenp = &_len;
  149.     buf = ptr;
  150.     bufp = &buf;
  151.     _flags |= _S_CAN_READ;
  152.     setb(buf, buf+_size);
  153.     if (pstart) {
  154.     _flags |= _S_CAN_WRITE;
  155.     setp(buf, buf+_size);
  156.     pbump(pstart-buf);
  157.     setg(buf, buf, pstart);
  158.     }
  159.     else {
  160.     setp(buf, buf); 
  161.     setg(buf, buf, buf+_size);
  162.     }
  163. }
  164.  
  165. strstreambuf::~strstreambuf()
  166. {
  167.     if (_frozen == 0)
  168.         free(buf);
  169. }
  170.